package com.ayu.a.chapter01;

import java.util.Arrays;
import java.util.List;

/**
 * @Author 徐林玉
 * @Description BinarySearch 二分查找
 * @Date 2022/11/3 10:36
 * @Version 1.0
 */
public class BinarySearch {


    public static void main(String[] args) {


        int imagineNum = getImagineNum(Arrays.asList(1, 5, 8, 14, 35, 36, 43, 55,77), 77);

        System.out.println("目标数据下标是 = " + imagineNum);


    }



    public static int getImagineNum(List<Integer> arr, Integer num ){
        int low = 0;
        int len = arr.size()-1;


        while (len-low>=0){

            int mid = (len+low)/2;

            Integer midValue = arr.get(mid);

            if(num>midValue){

                low = mid + 1;

            }else if(num.equals(midValue)){

                return mid;

            }else{
                len = mid-1;
            }

        }


        return -1;

    }
}
